package dao;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import entity.Lab;
import entity.LabAvailableTime;
import util.JDBCUtil;

public class LabDAO {
    // 检查实验室ID是否已存在
    public boolean checkLabIdExists(int labId) {
        String sql = "SELECT COUNT(*) FROM lab WHERE lab_id = ?";
        try (Connection conn = JDBCUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, labId);
            ResultSet rs = pstmt.executeQuery();
            rs.next();
            return rs.getInt(1) > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    // 添加实验室
    public boolean addLab(Lab lab) {
        if (checkLabIdExists(lab.getLabId())) {
            System.out.println("错误：实验室ID已存在");
            return false;
        }
        String sql = "INSERT INTO lab (lab_id, lab_number, lab_name, location, status, capacity) VALUES (?, ?, ?, ?, ?, ?)";
        try (Connection conn = JDBCUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, lab.getLabId());
            pstmt.setString(2, lab.getLabNumber());
            pstmt.setString(3, lab.getLabName());
            pstmt.setString(4, lab.getLocation());
            pstmt.setString(5, lab.getStatus());
            pstmt.setInt(6, lab.getCapacity());
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    // 删除实验室
    public boolean deleteLab(int labId) {
        String sql = "DELETE FROM lab WHERE lab_id = ?";
        try (Connection conn = JDBCUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, labId);
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    // 修改实验室信息
    public boolean updateLab(Lab lab) {
        String sql = "UPDATE lab SET lab_number=?, lab_name=?, location=?, status=?, capacity=? WHERE lab_id=?";
        try (Connection conn = JDBCUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, lab.getLabNumber());
            pstmt.setString(2, lab.getLabName());
            pstmt.setString(3, lab.getLocation());
            pstmt.setString(4, lab.getStatus());
            pstmt.setInt(5, lab.getCapacity());
            pstmt.setInt(6, lab.getLabId());
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    // 列出所有实验室
    public List<Lab> listAllLabs() {
        List<Lab> labs = new ArrayList<>();
        String sql = "SELECT * FROM lab";
        try (Connection conn = JDBCUtil.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {
            while (rs.next()) {
                labs.add(new Lab(
                        rs.getInt("lab_id"),
                        rs.getString("lab_number"),
                        rs.getString("lab_name"),
                        rs.getString("location"),
                        rs.getString("status"),
                        rs.getInt("capacity"),
                        rs.getTimestamp("create_time")
                ));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return labs;
    }

    // 根据ID获取实验室
    public Lab getLabById(int labId) {
        String sql = "SELECT * FROM lab WHERE lab_id = ?";
        try (Connection conn = JDBCUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, labId);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                return new Lab(
                        rs.getInt("lab_id"),
                        rs.getString("lab_number"),
                        rs.getString("lab_name"),
                        rs.getString("location"),
                        rs.getString("status"),
                        rs.getInt("capacity"),
                        rs.getTimestamp("create_time")
                );
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public boolean addAvailableTime(LabAvailableTime time) {
        String sql = "INSERT INTO lab_available_time (lab_id, start_time, end_time) VALUES (?, ?, ?)";
        try (Connection conn = JDBCUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, time.getLabId());
            pstmt.setTimestamp(2, new Timestamp(time.getStartTime().getTime()));
            pstmt.setTimestamp(3, new Timestamp(time.getEndTime().getTime()));
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            System.out.println("错误：时间段已存在");
            e.printStackTrace();
        }
        return false;
    }

    // 删除实验室可用时间段
    public boolean deleteAvailableTime(int timeId) {
        String sql = "DELETE FROM lab_available_time WHERE time_id = ?";
        try (Connection conn = JDBCUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, timeId);
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    // 根据实验室ID查询可用时间段
    public List<LabAvailableTime> getAvailableTimesByLabId(int labId) {
        List<LabAvailableTime> times = new ArrayList<>();
        String sql = "SELECT * FROM lab_available_time WHERE lab_id = ?";
        try (Connection conn = JDBCUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, labId);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                times.add(new LabAvailableTime(
                        rs.getInt("time_id"),
                        rs.getInt("lab_id"),
                        rs.getTimestamp("start_time"),
                        rs.getTimestamp("end_time")
                ));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return times;
    }


}
